package threadUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * @program: javase
 * @description: 信号量 1.用于共享资源互斥使用 2.并发限流
 * @author: lld
 * @create: 2021-04-18 20:04
 **/
public class SemaphoreTest {
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(3);
        for (int i = 1; i <= 9; i++) {
            final int num = i;
            new Thread(() -> {
                try {
                    semaphore.acquire();
                    System.out.println(num + "获取到停车位");
//                    假设使用一段时间，释放
                    TimeUnit.SECONDS.sleep(5);
//                    释放
                    System.out.println(num + "走了");
                    semaphore.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}
